package com.gqc.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gqc.bean.Commodity;
import com.gqc.service.CommodityService;

import net.sf.json.JSONObject;

@Controller
public class CommodityController {
	@Autowired
	CommodityService commodityService;

	@GetMapping("/queryBybusiness")
	@ResponseBody
	public JSONObject  HelloSpring(	@RequestParam("id")String id, @RequestParam("page")String page,@RequestParam("limit")String limit,HttpSession session) {
		JSONObject json = new JSONObject();
		 List<Commodity> list=commodityService.queryBybusiness(id);
		 session.setAttribute("list",list);
		 json.put("msg", "success");
	        json.put("code", "0");
	        json.put("data", list);
	        json.put("count", list.size());
		  return json;
		 
	}

	@RequestMapping("/queryBybusiness2")
	public String  HelloSpring(String id,HttpSession session) {
		 
		List<Commodity> list=commodityService.queryBybusiness(id);
		 session.setAttribute("list2", list);
		  return "user";
		 
	}
	
	
	 
	 
	 
	@RequestMapping("/findCommodityByUsername")
	@ResponseBody
	public List<Commodity> findCommodityByUsername(HttpServletRequest request,@RequestBody String username) {
		return commodityService.findCommodityByUsername(username);
	}
	
	@RequestMapping("/deleteCommodityById")
	@ResponseBody
	public boolean deleteCommodityById(@RequestParam("id") int id) {
		commodityService.deleteCommodityById(id);
		return true;
	}
	@RequestMapping("/insert")
	@ResponseBody
	public boolean insert(@RequestBody Commodity commodity) {
		commodityService.insert(commodity);
		return true;
	}
	@RequestMapping("/updateByPrimaryKeySelective")
	@ResponseBody
	public boolean updateByPrimaryKeySelective(@RequestParam("id")int id,@RequestParam("business") int business,@RequestParam("name")String name,@RequestParam("describe")String describe,@RequestParam("price")int price ) {
		Commodity commodity = new Commodity();
		commodity.setBusiness(business);
		commodity.setDescribe(describe);
		commodity.setId(id);
		commodity.setName(name);
		commodity.setPrice(price);
		commodityService.updateByPrimaryKeySelective(commodity);
		return true;
	}

}
